class EventsController < ApplicationController
  before_filter :get_parent, :only => [:new, :create, :index, :weekly, :today]

  def get_parent
    if params.include?(:location_id)
      @parent = Location.find(params[:location_id])
    end
  end

  # GET /events
  # GET /events.xml
  def index
    if @parent
      @events = @location.events
    else
      @events = Event.all
    end
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events }
    end
  end

  def search
#    if request.xhr?
      #Help to remove any empty queries fired off
      if params[:query].blank?
        @events = Array.new
      else
        @events = Event.find_with_ferret("*"+params[:query]+"*", {:limit => 15})
      end
      render :partial => 'search_results'
 #   end



  end
  def weeks
    @weeks = Array.new
    sunday = Date.today - Date.today.wday()
    0.upto(3) do |i|
      @weeks << sunday
      saturday = sunday + 6
      @weeks << saturday
      sunday = sunday + 7
    end

    respond_to do |format|
      format.html { render :json => @weeks }
    end
  end

  
  def today

    if @parent
      @events = Event.find(:all, :conditions => ['(start > NOW() OR (start < NOW() AND end > NOW())) AND start < DATE_ADD(CURDATE(), INTERVAL 1 DAY) AND location_id = ? ', params[:location_id]], :order => :start)
    else
      @events = Event.find(:all, :conditions => ['start >= CURDATE() AND start <= CURDATE()'], :order => :start)
    end
    

  end

  def weekly

    if params.include?(:year) and params.include?(:month) and params.include?(:day)
      requested_date = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
    else
      requested_date = Date.today
    end

    @first_day = requested_date - requested_date.wday()
    @last_day = @first_day + 6
    if @parent
      @events = Event.find(:all, :conditions => ['start >= ? AND start <= ? AND location_id = ?', @first_day, @last_day, params[:location_id]], :order => :start)
    else
      @events = Event.find(:all, :conditions => ['start >= ? AND start <= ?', @first_day, @last_day], :order => :start)
    end

    respond_to do |format|
      if (request.xhr?)
        format.html { render :layout => false }
      end
      format.html
    end

  end



  # GET /events/1
  # GET /events/1.xml
  def show
    @event = Event.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/new
  # GET /events/new.xml
  def new
    @event = Event.new
    @locations = Location.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
    @locations = Location.all

  end

  # POST /events
  # POST /events.xml
  def create
    @event = Event.new(params[:event])
    respond_to do |format|
      if @event.save
        flash[:notice] = 'Event was successfully created.'
        format.html { redirect_to(@event) }
        format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])

    respond_to do |format|
      if @event.update_attributes(params[:event])
        flash[:notice] = 'Event was successfully updated.'
        format.html { redirect_to(@event) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  end
end
